/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.map;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Susanta Tewari
 */
public class FullContigMap extends ContigMap {

    /**
     * Constructs ...
     *
     *
     * @param probeMap
     */
    public FullContigMap(final ProbeMap probeMap) {
        super(probeMap);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    protected List<Contig> buildContigMap() {

        final List<String> probes = getMapData().getModel().getProbes();
        final List<Contig> result = new ArrayList<>(probes.size());
        Set<String> set1;
        final List<String> allClones = new ArrayList<>(getMapData().getSampleSize());
        Contig contig                = new Contig();

        for (int i = 0; i < probes.size() - 1; i++) {

            contig.addProbe(probes.get(i));
            allClones.add(probes.get(i));

            set1 = getMapData().getConnectingClones(probes.get(i));

            final Set<String> set2 = getMapData().getConnectingClones(probes.get(i),
                                         probes.get(i + 1));

            set1.removeAll(set2);
            set1.removeAll(allClones);
            set2.removeAll(allClones);
            contig.addClones(new ArrayList<>(set1));
            contig.addClones(new ArrayList<>(set2));
            allClones.addAll(set1);
            allClones.addAll(set2);

            if (set2.isEmpty()) {

                result.add(contig);

                contig = new Contig();
            }
        }


        // last probe
        contig.addProbe(probes.get(probes.size() - 1));

        set1 = getMapData().getConnectingClones(probes.get(probes.size() - 1));

        set1.removeAll(allClones);
        contig.addClones(new ArrayList<>(set1));
        result.add(contig);

        return result;
    }
}
